//自定义keepAliveWrapper TabBar案例
import 'package:flutter/material.dart';
import '../../tools/keepAliveWrapper.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  TabController? _tabController;
  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 9, vsync: this);
    //监听_tabController 改变事件 获取动态数据
    _tabController?.addListener(() {
      // print("当前索引：${_tabController?.index}");//可能会触发多次
      if (_tabController?.animation?.value == _tabController?.index) {
        print("当前索引：${_tabController?.index}");
      }
    });
  }

  //生命周期函数 ---  销毁监听
  @override
  void dispose() {
    super.dispose();
    _tabController?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Scaffold(
        appBar: PreferredSize(
          //可以配置appbar的高度
          preferredSize: Size.fromHeight(40),
          child: AppBar(
            // elevation: 0.5,
            title: SizedBox(
              height: 40,
              child: TabBar(
                // onTap:(index){//只能监听点击事件 没法监听滚动事件
                //   print("点击了$index");
                // },
                isScrollable: true,
                controller: _tabController,
                labelStyle: TextStyle(fontSize: 15),
                // indicatorPadding: EdgeInsets.all(10),
                indicatorSize: TabBarIndicatorSize.tab,
                indicatorColor: Colors.red,
                labelColor: Colors.red,
                unselectedLabelColor: Colors.black,
                unselectedLabelStyle: TextStyle(fontSize: 14),
                tabs: [
                  Tab(text: '推荐'),
                  Tab(text: '视频'),
                  Tab(text: '图片'),
                  Tab(text: '本地'),
                  Tab(text: '社会'),
                  Tab(text: '军事'),
                  Tab(text: '科技'),
                  Tab(text: '财经'),
                  Tab(text: '国际'),
                ],
              ),
            ),
            backgroundColor: Colors.white,
            // centerTitle: true,
            // bottom:
          ),
        ),

        body: TabBarView(
          controller: _tabController,
          children: [
            //缓存推荐列表
            KeepAliveWrapper(
              child: ListView(
                children: [
                  ListTile(title: Text('推荐1')),
                  ListTile(title: Text('推荐2')),
                  ListTile(title: Text('推荐3')),
                  ListTile(title: Text('推荐4')),
                  ListTile(title: Text('推荐6666')),
                  ListTile(title: Text('推荐')),
                  ListTile(title: Text('推荐6666')),
                  ListTile(title: Text('推荐6')),
                  ListTile(title: Text('推荐7')),
                  ListTile(title: Text('推荐8')),
                  ListTile(title: Text('推荐8')),
                  ListTile(title: Text('推荐9')),
                  ListTile(title: Text('推荐10')),
                  ListTile(title: Text('推荐')),
                  ListTile(title: Text('推荐3333')),
                ],
              ),
            ),
            Text('视频'),
            Text('图片'),
            Text('本地'),
            Text('社会'),
            Text('军事'),
            Text('科技'),
            Text('财经'),
            Text('国际'),
          ],
        ),
      ),
    );
  }
}
